library(tidyverse)
library(magrittr)
library(osmdata)
library(sfnetworks)
library(units)
library(sf)
library(tidygraph)
library(tmap)
library(here)
ttm()
We are going to do quick analyses using the semantic segmentation model results.
Merge the data processed from Colab back to R
If you are having trouble running the Colab script, you may use these files: geojson and csv.
# Load the sampled points.
edges_azi <- st_read(????) # path to your "edges_azi_{city_name}.geojson" file
# Download the output from the computer vision models.
seg_output <- read.csv(????) # path to your "seg_output.csv" file
# Join them back to the `edges_azi` object that was used to download images.
edges_seg_output <- edges_azi %>%
inner_join(seg_output, by=c("node_id"="img_id"))
The numeric values in each object column represent the number of pixels in images predicted as that object. Check out the first few rows and see which objects comprise the majority of streetscapes.
head(edges_seg_output)
## Simple feature collection with 6 features and 159 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -84.31352 ymin: 33.85624 xmax: -84.28065 ymax: 33.86759
## Geodetic CRS: WGS 84
## type X Y azi osm_id highway length edge_id
## 1 start -84.28108 33.86751 -161.98023 9164345 residential 109.5059 1
## 2 mid1 -84.28096 33.86717 65.66688 9164345 residential 109.5059 1
## 3 mid2 -84.28096 33.86717 245.66688 9164345 residential 109.5059 1
## 4 end -84.28065 33.86759 205.27772 9164345 residential 109.5059 1
## 5 start -84.31352 33.85658 123.81208 9164886 residential 128.4166 2
## 6 mid1 -84.31295 33.85624 114.20805 9164886 residential 128.4166 2
## node_id wall building sky floor tree ceiling road bed windowpane grass
## 1 1 0 43156 132701 0 41387 0 169173 0 0 0
## 2 2 0 47933 143970 0 27843 0 160254 0 0 0
## 3 3 234 44718 115545 0 53665 0 173885 0 0 9
## 4 4 0 20873 150001 0 39947 0 176363 0 0 0
## 5 5 0 1082 138464 0 47444 0 180533 0 0 16314
## 6 6 0 14 139586 0 53002 0 178227 0 0 1464
## cabinet sidewalk person earth door table mountain plant curtain chair car
## 1 0 5 0 65 0 0 0 54 0 0 22653
## 2 0 1330 0 0 0 0 1695 32 0 0 26543
## 3 0 3111 33 0 0 0 0 12438 0 0 5962
## 4 0 53 0 0 0 0 0 0 0 0 12952
## 5 0 5106 0 11610 0 0 0 421 0 0 0
## 6 0 16111 0 9830 0 0 0 118 0 0 798
## water painting sofa shelf house sea mirror rug field armchair seat fence desk
## 1 0 0 0 0 406 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0 0 0 0 65 0
## 6 0 0 0 0 0 0 0 0 0 0 0 0 0
## rock wardrobe lamp bathtub railing cushion base box column signboard
## 1 0 0 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0 0 5738
## 6 0 0 0 0 0 0 0 0 0 6614
## chest.of.drawers counter sand sink skyscraper fireplace refrigerator
## 1 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0
## grandstand path stairs runway case pool.table pillow screen.door stairway
## 1 0 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0 0
## river bridge bookcase blind coffee.table toilet flower book hill bench
## 1 0 0 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0 0 0
## countertop stove palm kitchen.island computer swivel.chair boat bar
## 1 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0
## arcade.machine hovel bus towel light truck tower chandelier awning
## 1 0 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0 0
## streetlight booth television.receiver airplane dirt.track apparel pole land
## 1 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0
## 5 556 0 0 0 0 0 2211 0
## 6 94 0 0 0 0 0 3742 0
## bannister escalator ottoman bottle buffet poster stage van ship fountain
## 1 0 0 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 9410 0 0
## 5 0 0 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0 0 0
## conveyer.belt canopy washer plaything swimming.pool stool barrel basket
## 1 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0
## waterfall tent bag minibike cradle oven ball food step tank trade.name
## 1 0 0 0 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0 0 0 0
## microwave pot animal bicycle lake dishwasher screen blanket sculpture hood
## 1 0 0 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0 0
## 4 0 0 0 1 0 0 0 0 0 0
## 5 0 0 0 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0 0 0
## sconce vase traffic.light tray ashcan fan pier crt.screen plate monitor
## 1 0 0 0 0 0 0 0 0 0 0
## 2 0 0 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 0 0 0 0 0
## 4 0 0 0 0 0 0 0 0 0 0
## 5 0 0 56 0 0 0 0 0 0 0
## 6 0 0 0 0 0 0 0 0 0 0
## bulletin.board shower radiator glass clock flag geometry
## 1 0 0 0 0 0 0 POINT (-84.28108 33.86751)
## 2 0 0 0 0 0 0 POINT (-84.28096 33.86717)
## 3 0 0 0 0 0 0 POINT (-84.28096 33.86717)
## 4 0 0 0 0 0 0 POINT (-84.28065 33.86759)
## 5 0 0 0 0 0 0 POINT (-84.31352 33.85658)
## 6 0 0 0 0 0 0 POINT (-84.31295 33.85624)
Calculate Greenness
Let’s define greenness as the sum of tree, grass, and plant.
edges_seg_output %<>% # `a %<>%` is the same as `a <- a %>%`; you need to import `magrittr` to use this pipe
mutate(greenness = ????,
pct_greenness = greenness/(640*640))
# Map!
t1 <- tm_basemap("OpenStreetMap")+
tm_shape(edges_seg_output) +
tm_dots(col = "pct_greenness", style="quantile", palette = 'viridis')
t2 <- tm_basemap(leaflet::providers$Esri.WorldImagery) +
tm_shape(edges_seg_output %>% st_bbox() %>% st_as_sfc()) +
tm_borders(lwd = 2, col = 'white') +
tmap_arrange(t1, t2, sync = T)
Calculate Building-to-street Ratio
Let’s calculate building-to-street ratio. Major ‘building’ objects include building and house. Major ‘street’ objects include road, sidewalk, and car.
edges_seg_output %<>%
mutate(b2s_ratio = ????) %>%
mutate(b2s_ratio = case_when(
b2s_ratio >= 1 ~ 1, # Let's set the upper limit as 1.
TRUE ~ b2s_ratio))
t1 <- tm_basemap("OpenStreetMap")+
tm_shape(edges_seg_output) +
tm_dots(col = "b2s_ratio", style="quantile", palette = 'viridis')
t2 <- tm_basemap(leaflet::providers$Esri.WorldImagery) +
tm_shape(edges_seg_output %>% st_bbox() %>% st_as_sfc()) +
tm_borders(lwd = 2, col = 'white')
tmap_arrange(t1, t2, sync = T)
Violin plots comparing the segmentation results by type of road
Are there significant differences between road types?
edges_seg_output %<>%
filter(!is.na(highway)) %>%
mutate(highway = factor(highway, levels = c('unclassified','residential','tertiary',
'secondary','primary','trunk'))) %>%
mutate(pct_building = building/(640*640),
pct_sky = sky/(640*640),
pct_road = road/(640*640),
pct_sidewalk = sidewalk/(640*640))
edges_seg_output %>%
pivot_longer(cols = c(b2s_ratio, pct_greenness, pct_building,
pct_sky, pct_road, pct_sidewalk),
names_to = 'variable',
values_to = "value") %>%
ggplot(mapping = aes(x = highway, y = value)) +
geom_violin(fill = "#c7ffd6") +
stat_summary(fun=mean, geom="point", size=1, color="red")+
coord_flip() +
theme_bw() +
facet_wrap(~variable, scales = "free_x", nrow = 2)
## Warning: Removed 295 rows containing non-finite values (`stat_ydensity()`).
## Warning: Removed 295 rows containing non-finite values (`stat_summary()`).